// ==UserScript==
// @name         成都住建预售与现售项目列表优化
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  优化成都住建预售与现售项目列表显示
// @author       You
// @match        https://www.cdzjryb.com/SCXX/Default.aspx?action=ucSCXXShowNew2
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 添加样式
    const style = document.createElement('style');
    style.textContent = `
        /* 表格样式优化 */
        #ID_ucSCXXShowNew2_gridView {
            border-collapse: collapse;
            width: 100%;
            margin: 15px 0;
            background: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 12px rgba(0,0,0,0.1);
        }

        #ID_ucSCXXShowNew2_gridView th {
            background: #308cde;
            color: white;
            padding: 12px 8px;
            font-weight: 500;
        }

        #ID_ucSCXXShowNew2_gridView td {
            padding: 10px 8px;
            border: 1px solid #eee;
        }

        #ID_ucSCXXShowNew2_gridView tr:hover {
            background-color: #f5f7fa;
        }

        /* 一房一价弹窗样式 */
        .price-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 9999;
        }

        .price-modal-content {
            position: relative;
            width: 95%;
            height: 95%;
            margin: 20px auto;
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.15);
        }

        .price-modal-close {
            position: absolute;
            right: 20px;
            top: 10px;
            font-size: 24px;
            color: #666;
            cursor: pointer;
            z-index: 10000;
        }

        .price-modal-close:hover {
            color: #333;
        }

        .price-modal iframe {
            width: 100%;
            height: 100%;
            border: none;
        }
    `;
    document.head.appendChild(style);

    // 创建弹窗容器
    const modal = document.createElement('div');
    modal.className = 'price-modal';
    modal.innerHTML = `
        <div class="price-modal-content">
            <span class="price-modal-close">×</span>
            <iframe></iframe>
        </div>
    `;
    document.body.appendChild(modal);

    // 处理一房一价链接点击
    const priceLinks = document.querySelectorAll('a[id*="hyPresLink"]');
    priceLinks.forEach(link => {
        link.addEventListener('click', (e) => {
            e.preventDefault();
            const url = link.href;
            modal.querySelector('iframe').src = url;
            modal.style.display = 'block';
        });
    });

    // 关闭弹窗
    modal.querySelector('.price-modal-close').addEventListener('click', () => {
        modal.style.display = 'none';
        modal.querySelector('iframe').src = '';
    });

    // 点击弹窗外区域关闭
    modal.addEventListener('click', (e) => {
        if (e.target === modal) {
            modal.style.display = 'none';
            modal.querySelector('iframe').src = '';
        }
    });

    // 优化表格内容显示
    const table = document.getElementById('ID_ucSCXXShowNew2_gridView');
    if (table) {
        // 设置表头固定宽度
        const headerCells = table.querySelectorAll('th');
        const widths = ['80px', '150px', '80px', '180px', '100px', '150px', '100px', '100px', '150px', '120px', '80px', '60px', '60px'];
        headerCells.forEach((cell, index) => {
            if (widths[index]) {
                cell.style.width = widths[index];
            }
        });

        // 处理长文本显示
        const cells = table.querySelectorAll('td');
        cells.forEach(cell => {
            if (cell.textContent.length > 20) {
                cell.title = cell.textContent;
                cell.style.maxWidth = '200px';
                cell.style.overflow = 'hidden';
                cell.style.textOverflow = 'ellipsis';
                cell.style.whiteSpace = 'nowrap';
            }
        });
    }
})();
